package cn.wk.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


public class UserFilter implements Filter {
    //销毁的时候被执行
    public void destroy() {
        System.out.println("销毁的时候被执行！");
    }
    //被执行多次
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        System.out.println("userFilter 的doFilter被执行了！");
        HttpServletRequest request= (HttpServletRequest)req;
        HttpServletResponse response=(HttpServletResponse) resp;
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        //放行
        chain.doFilter(req, resp);
    }
    //创建的时候 执行一次
    public void init(FilterConfig config) throws ServletException {
        System.out.println("init 被执行了！");
    }

}
